package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendanceMonthlyArchiveAddDTO;
import org.jsola.hr.dto.AttendanceMonthlyArchiveUpdateDTO;
import org.jsola.hr.query.AttendanceMonthlyArchiveQuery;
import org.jsola.hr.service.IAttendanceMonthlyArchiveService;
import org.jsola.hr.vo.AttendanceMonthlyArchiveListVO;
import org.jsola.hr.vo.AttendanceMonthlyArchiveVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 员工考勤月统计归档
 *
 * @author zhr
 */
@RestController("hrAttendanceMonthlyArchiveController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendanceMonthlyArchives")
public class AttendanceMonthlyArchiveController implements IAttendanceMonthlyArchiveControllerAPI {

    @Autowired
    private IAttendanceMonthlyArchiveService attendanceMonthlyArchiveService;

    @Override
    @PostMapping
    public Result<AttendanceMonthlyArchiveVO> save(@Valid @RequestBody AttendanceMonthlyArchiveAddDTO attendanceMonthlyArchiveAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工考勤月统计归档权限校验

        return Result.success(
                attendanceMonthlyArchiveService.save(attendanceMonthlyArchiveAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendanceMonthlyArchiveUpdateDTO attendanceMonthlyArchiveUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工考勤月统计归档权限校验

        int count = attendanceMonthlyArchiveService.update(attendanceMonthlyArchiveUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{attendanceMonthlyArchiveId}")
    public Result<Integer> delete(@PathVariable String attendanceMonthlyArchiveId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工考勤月统计归档权限校验

        int count = attendanceMonthlyArchiveService.deleteByIds(tokenUser, attendanceMonthlyArchiveId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] attendanceMonthlyArchiveIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工考勤月统计归档权限校验

        int count = attendanceMonthlyArchiveService.deleteByIds(tokenUser, attendanceMonthlyArchiveIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{attendanceMonthlyArchiveId}")
    public Result<AttendanceMonthlyArchiveVO> findById(@PathVariable String attendanceMonthlyArchiveId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤月统计归档权限校验

        return Result.success(
                attendanceMonthlyArchiveService.selectById(attendanceMonthlyArchiveId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AttendanceMonthlyArchiveListVO>> listAll(@Valid AttendanceMonthlyArchiveQuery attendanceMonthlyArchiveQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤月统计归档权限校验

        return Result.success(
                attendanceMonthlyArchiveService.select(attendanceMonthlyArchiveQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AttendanceMonthlyArchiveListVO>> page(@Valid AttendanceMonthlyArchiveQuery attendanceMonthlyArchiveQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                attendanceMonthlyArchiveService.selectPage(attendanceMonthlyArchiveQuery, tokenUser.getSiteId())
        );
    }


}

